#pragma once

#include <core/serialize/serialize.h>
#include <widgets/SDragListWidget.h>
#include <widgets/SImageButton.h>
#include <widgets/SPopupWindow.h>

#include <QListView>
#include <QSpacerItem>
#include <QStandardItemModel>
#include <QStyledItemDelegate>
#include <QTableView>
#include <map>
#include <vector>

// tab1
class SpecialLabelItemDelegate : public QStyledItemDelegate {
  Q_OBJECT
 public:
  SpecialLabelItemDelegate(QObject* p, int count);
  virtual void paint(QPainter* painter, const QStyleOptionViewItem& option,
                     const QModelIndex& index) const override;
  void hitCheck(int row);
  void check(int row, bool check);
  void getCheck(std::vector<int>& vctCheck);
  void clearCheck();

 protected:
  bool isLabelFlag(const QString& str, const QString& id) const;

  struct tagSPCheck {
    tagSPCheck() : check(false) {}
    bool check;
    QRect rect;
  };
  mutable std::vector<tagSPCheck> m_vctChecks;
};

class SpecialLabelConfig {
 public:
  static SpecialLabelConfig* GetInstance();
  bool IsSaved() const;
  bool CanShow(const std::string& id) const;
  void Save(const std::vector<std::string>& ids);

 private:
  bool Load();
  void Save();

 private:
  bool m_isSaved;
  std::vector<std::string> m_vecIDs;
  QString m_configPathName;
  SpecialLabelConfig();
};

// tab2
struct Model_CopySequenceCommon : public Serialize {
  std::vector<int> m_selects;
  virtual bool GetCardName(QString& result, int index) { return true; }
  virtual int GetAllCardsCnt() { return 0; }
  virtual void Reset() {}
};

struct Model_CopySequence : public SerializeUpper {
  Json::Value Wrapper() const override;
  void Parser(Json::Value& input) override;

  struct Model_CopySequenceTab : public Model_CopySequenceCommon {
    Json::Value Wrapper() const override;
    void Parser(Json::Value& input) override;
    bool GetCardName(QString& result, int index) override;
    int GetAllCardsCnt() override;
    void Initialize();
    void Reset() override;
    int m_type{0};

    enum col {
      colMty,
      colTicker,
      colName,
      colBidOfr,
      colVolBidOfr,
      colRating,
      colPledge,
      colTrade,
      colClose,
      colTradeClose,
      colIssuer,
      colGuarantor,
      colIssueScale,
      colAmount,
      colMunicipalBondType,
      colRegion,
      colIssueMethod,
      colCdc,
      colCdcDuration,
      colCdcRating,
      colCsi,
      colCouponRate,
      colMuturityDate,
      colBroker,
      colTime,
      colDate,
      colTradeCdc,
      colBidCdc,
      colCdcOfr,
      colMax
    };
  };
  enum tab { tabTreasury, tabLocal, tabCredit, tabNCD, tabMax };
  std::map<int, Model_CopySequenceTab> m_tabs;
};

////////////////////////////////////////////////////////////////////

class STabCtrl;
class SubSpecialLabelDlg : public SPopupWindow {
  struct tagField {
    QString str;
    QColor color;
  };

  struct tagLineValue {
    QString id;
    QString label;
    QColor color;
    bool check;
    std::vector<tagField> comments;
    tagLineValue() : check(false) {}
  };

  Q_OBJECT
 public:
  SubSpecialLabelDlg(QWidget* p = nullptr);

 protected Q_SLOTS:
  void onSpecialLabelCheckClick(const QModelIndex& index);

 protected:
  void paintEvent(QPaintEvent* event) override;
  void resizeEvent(QResizeEvent* event) override;
  void mousePressEvent(QMouseEvent* event) override;

  void onBtnOk();
  void onBtnCancel();
  void onBtnApply();
  void onQuoteCopyTabSel();
  void onQuoteCopyUnSelCheck();
  void onQuoteCopySelCheck();

  void loadQuoteCopy();
  bool splitComments(const QString& str, std::vector<tagField>& vecField);
  bool hexStrToInt(const char* str, int len, QColor& value);

 private:
  QSpacerItem* m_spacer = nullptr;
  QTableView* m_tableView = nullptr;
  QStandardItemModel* m_model = nullptr;
  SpecialLabelItemDelegate* m_delegate = nullptr;
  STabCtrl* m_QuoteCopyTab = nullptr;
  QWidget* m_BondCopyPanel = nullptr;
  QListView* m_listUnSel = nullptr;
  SDragListWidget* m_listSel = nullptr;
  SImageButton* m_btnSel = nullptr;
  SImageButton* m_btnUnSel = nullptr;
  Model_CopySequence m_reqImpl;
  QRect m_rcSpecialLabel;
  QRect m_rcQuoteCopy;
  int m_nLeftPanelSel = 0;
  int m_nQuoteCopyTabSel = 0;
  std::vector<tagLineValue> m_datas;
  std::vector<int> m_selects;
  struct tagQCCheck {
    std::vector<int> vctUnSel;
    std::vector<int> vctSel;
  };
  tagQCCheck m_quoteCopy[Model_CopySequence::tabMax];
  QStandardItemModel* m_modelUnSel[Model_CopySequence::tabMax];
  SDragListItemModel* m_modelSel[Model_CopySequence::tabMax];
};